*** Social Insurance Coverage ***
* Please use STATA 14 or more advanced versions.

global path "C:\Users\haoch\Dropbox (Personal)\Research\Social Insurance\Zhang and Zhang 2025 Replication\main text\Figure 1"
use "$path\result.dta", clear

***** Map plots (only for the SAT agencies)
*** Step 1. Transform .shp and .dbf files into .dta files
shp2dta using "$path/China_prefecture_map_shapefile/CN-shi-A", ///
database("$path/China_prefecture_map_shapefile/cncitydb") ///
coordinates("$path/China_prefecture_map_shapefile/cncitycoord") genid(_ID) replace

*** Step 2. Fix the chinese coding 
clear 
cd "$path/China_prefecture_map_shapefile"
cap unicode analyze "cncitydb.dta"
cap unicode encoding set "GB18030"
cap unicode retranslate "cncitydb.dta", transutf8

use "$path/China_prefecture_map_shapefile/cncitydb.dta", clear 
cap gen citycode = SHI/100
order citycode, after(SHI)
// drop if CityNameC=="台湾" | CityNameC=="香港" | CityNameC=="澳门"
replace citycode=1201 if citycode==1202
replace citycode=1101 if citycode==1102
// drop if citycode==0
save "$path/China_prefecture_map_shapefile/cncitydb_used.dta", replace

*** Step 3. Merge the insurance coverage data with borders and coordinates:

	use "$path/result.dta", clear
	sort citycode
	merge 1:m citycode using "$path/China_prefecture_map_shapefile/cncitydb_used.dta", generate(_merge)
	// drop if _merge!=3
	sort citycode
	gen unemployment1 = int(unemployment*100)
	gen medical1 = int(medical*100)
	save "$path/result_map_used.dta", replace

*** Step 4. Draw the map
* 2007
use "$path/result_map_used.dta", clear
spmap unemployment1 using "$path/China_prefecture_map_shapefile/cncitycoord", ///
id(_ID) fcolor(Reds2) ocolor(black ..) osize(thin ..) ///
ndfcolor(white) ndocolor(black) ndsize(thin ..) ///
plotregion(icolor(eggshell)) graphregion(icolor(eggshell)) ///
cln(6) legstyle(1) legend(ring(1) position(3) size(*0.7) cols(1) region(lstyle(none))) ///
title("Unemployment Insurance (2007)", size(small)) ///
plotregion(margin(7 -5 7 -1)) norescaling
graph export "$path/Unemployment_Insurance.pdf", as(pdf) replace /* cut space at the bottom and output png to save space */
graph drop _all    /* erase stata graphs to save space */

use "$path/result_map_used.dta", clear
spmap medical1 using "$path/China_prefecture_map_shapefile/cncitycoord", ///
id(_ID) fcolor(Greens2) ocolor(black ..) osize(thin ..) ///
ndfcolor(white) ndocolor(black) ndsize(thin ..) ///
plotregion(icolor(eggshell)) graphregion(icolor(eggshell)) ///
cln(6) legstyle(1) legend(ring(1) position(3) size(*0.7) cols(1) region(lstyle(none))) ///
title("Medical Insurance and Pensions (2007)", size(small)) ///
plotregion(margin(7 -5 7 -1)) norescaling
graph export "$path/Medical_Insurance.pdf", as(pdf) replace /* cut space at the bottom and output png to save space */
graph drop _all    /* erase stata graphs to save space */
